<template>
  <div>
    <AplidForm ref="formRef" v-bind="recordPart" v-model="formModel" @submit="onSubmit">
    </AplidForm>
  </div>
</template>

<script setup lang="ts">
import { cloneDeep } from 'lodash-es'
import { recordPart } from '@/views/datacenter/basicData/serviceFacility/config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { getValue, getContent } = useOperate(3)
const { setTempParams } = useCommonStore()
const emit = defineEmits(['submit'])
const formRef = ref(null)

const rowData: any = inject('nowRowData')
// dpServiceFacilityObject
// dpFacilityEquipmentList
// dpOperateRecordList
// dpSenileProductsList
// dpServiceFacilityExtend
// dpServiceFacilityOther
let otherMod = rowData.value?.data?.dpServiceFacilityOther
let baseData = rowData.value?.baseData?.dpServiceFacilityOther || {}

let recordData = rowData.value?.data?.dpOperateRecordList
let baseRecordData = rowData.value?.baseData?.dpOperateRecordList || {}


const formModel = ref({
  ...cloneDeep(otherMod),
  dpOperateRecordPOList: recordData
})
const onSubmit = (form, done) => {
  const { tempNowObj } = storeToRefs(useCommonStore())
  // 处理几个列表数据
  form.dpOperateRecordPOList.forEach(el => {
    el.soleValue = tempNowObj.value.name,
      el.code = 2013
  })
  const params = {
    ...form,
    soleValue: tempNowObj.value.name,
    code: 2004
  }
  // 处理变更字段
  // 变更字段
  let changeColumnsBase = recordPart.column.filter(
    (item) => item.changeField
  )
  // 得到变更后的老人用品，设施，和运营记录
  let changeData:any = []
  changeColumnsBase.map((item) => {
    const tempBefore = baseData[item.propName || item.prop]
    const tempAfter = params[item.propName || item.prop]
    if (tempBefore != tempAfter) {
      changeData.push({
        label: item.label,
        before: getValue(tempBefore, item),
        after: getValue(tempAfter, item)
      })
    }
  })

  const changeObj = {
    data: changeData,
    dataChangeContentList: [
      ...getContent(baseRecordData, params.dpOperateRecordPOList, 2013, '养老设施-运营记录', { code: undefined }, 'id'),
      {
        changeBefore: {
          ...baseData,
          dpFacilityEquipmentPOList: undefined,
          dpSenileProductsPOList: undefined,
          dpOperateRecordPOList: undefined,
          code: undefined
        },
        changeAfter: {
          ...params,
          dpFacilityEquipmentPOList: undefined,
          dpSenileProductsPOList: undefined,
          dpOperateRecordPOList: undefined,
          code: undefined
        },
        changeData: {
          ...params,
          code: 2004
        },
        changeType: '养老服务设施其他信息',
        code: 2004
      }
    ],

  }
  setTempParams({
    name: params.soleValue,
    nowChange: changeObj
  })
  emit('submit', changeObj)
}

const onSave = () => {
  formRef.value!.onSubmit()
}
defineExpose({
  onSave
})
</script>

<style scoped></style>
